package com.example.assetmanagement.service.impl;

import com.example.assetmanagement.common.PageRequest;
import com.example.assetmanagement.common.PageResponse;
import com.example.assetmanagement.domain.MaintenancePlan;
import com.example.assetmanagement.mapper.MaintenancePlanMapper;
import com.example.assetmanagement.service.MaintenancePlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MaintenancePlanServiceImpl implements MaintenancePlanService {

    private final MaintenancePlanMapper planMapper;

    @Override
    public PageResponse<MaintenancePlan> list(PageRequest pageRequest, String type, Integer status, String startDate, String endDate) {
        List<MaintenancePlan> list = planMapper.selectPage(pageRequest.getOffset(), pageRequest.getSize(), type, status, startDate, endDate);
        long total = planMapper.count(type, status, startDate, endDate);
        return new PageResponse<>(total, list);
    }

    @Override
    @Transactional
    public void add(MaintenancePlan plan) {
        planMapper.insert(plan);
    }

    @Override
    @Transactional
    public void update(Long id, MaintenancePlan plan) {
        plan.setId(id);
        planMapper.update(plan);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        planMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void batchDelete(List<Long> ids) {
        if(ids!=null && !ids.isEmpty()) planMapper.deleteBatch(ids);
    }

    @Override
    @Transactional
    public void updateStatus(Long id, Integer status, Integer progress) {
        planMapper.updateStatus(id, status, progress);
    }
} 